﻿using AdverseEventReportSystem.BLL.Escort;
using AdverseEventReportSystem.BLL.EscortManageSys;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace AdverseEventReportSystem.Web.Areas.Escort.Controllers
{
    public class TodayTrajectoryController : BaseController
    {
        //
        // GET: /Escort/TodayTrajectory/
        public ActionResult Index()
        {
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfString serviceResult = service.GetAllParameterDict();
            if (serviceResult.S)
            {
                DataTable jsonobj = JsonHelper.ToDataTable(serviceResult.D);
                DataRow row = jsonobj.Select(" PARA_NAME = 'TEMPERATURE_ALERT_NUM' ")[0];
                ViewBag.dis_temper = row["DEFAULT_VALUE"].ToString();
            }
            ViewBag.CurrDepCode = loginUser.CurrDepCode;
            ViewBag.JobTitle = loginUser.JobTitle;
            return View();
        }

        public ActionResult Search()
        {
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", ConfigHelper.GetConfigInt("pagesize"));
            string date = WebUtils.GetString("date");
            string departmentCode = WebUtils.GetString("departmentCode");
            if (departmentCode == "-1") departmentCode = "";
            int tiwenzhuangtai = WebUtils.GetInt("tiwenzhuangtai");
            int sfxc = WebUtils.GetInt("sfxc");
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfString serviceResult = service.GetEscortTempratureByDept(departmentCode, tiwenzhuangtai,sfxc ,pageIndex, pageSize);
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                var result = new { Total = jsonobj.total, Rows = jsonobj.data };
                return JsonDate(result);
            }

            return JsonDate(new { Total = 0, Rows = new DataTable() });
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 添加/编辑体温记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="jsonInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(string id, string jsonInfo)
        {
            OperateResult resultInfo = TodayTrajectoryManager.Save(id, loginUser.Personnel_id, jsonInfo);
            return JsonDate(resultInfo);
        }

        public ActionResult GetEscortPathInfo()
        {
            string cid = Request["esid"];
            string date = Request["date"];
            if (date == "") date = DateTime.Now.ToString("yyyy-MM-dd");

            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfString serviceResult = service.GetEscortPathInfo(cid, date);
            if (serviceResult.S)
            {
                var jsonobj = JsonConvert.DeserializeObject<dynamic>(serviceResult.D);
                return JsonDate(jsonobj);
            }

            return JsonDate(new { Total = 0, Rows = new DataTable() });
        }

        public ActionResult Export()
        {
            string date = Request["date"];
            if (date == "") date = DateTime.Now.ToString("yyyy-MM-dd");
            string depid = Request["depid"];
            if (depid == "-1") depid = string.Empty;
            int temper = Convert.ToInt32(Request["temper"]);
            int sfxc = WebUtils.GetInt("sfxc");
            EscortManageSysSoapClient service = new EscortManageSysSoapClient();
            ResultHelperOfArrayOfByte serviceResult = service.GetEscortTempForExport(depid, date, temper, sfxc);
            if (serviceResult.S)
            {
                string fileName = ("陪护人员体温记录-") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                return File(serviceResult.D, "application/vnd.ms-excel", fileName);
            }
            return View();
        }
    }
}